package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.zhounianturntable.ZhouNianTurntableMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ZhouNianTurntableRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = ZhouNianTurntableMgr.getInTimeActivityInfo();
        if (activityInfo != null) {
            long recycleTime = activityInfo.getEndShowTime() * 1000 + 600 * 1000;
            //标记道具
            List<String> paramList = new ArrayList<>();
            paramList.add(ZhouNianTurntableMgr.activityMap.get(activityInfo.getActivityId()).ZHOUNIAN_TURNTABLE_RECYCLE);
            ActivityItemRecycleMgr.addRecycleItemData(ZhouNianTurntableMgr.activityMap.get(activityInfo.getActivityId()).ZHOUNIAN_TURNTABLE_ITEM_ID, activityType, 1, recycleTime, paramList);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.ZhouNianTurntable.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        Property reward = new Property();
        UserBagItem bagItem = getUserBagItem(userId, itemId);
        long num = 0;
        if (bagItem != null && bagItem.getNum() > 0) {
            num = bagItem.getNum();
            if (removeItem(bagItem)) {
                // 转换道具
                for (Map.Entry<Integer, BigInteger> entry : PropertyHelper.parseStringToProperty(data.getParamList().get(0)).getGoods().entrySet()) {
                    reward.addProperty(new Property(entry.getKey(), entry.getValue().multiply(BigInteger.valueOf(num))));
                }
            }
        }
        if (num > 0 && !reward.isNothing()) {
            String language = UserMgr.getLanguage(userId);
            String title = MultipleLanguageMgr.getContent(MailManager.ZHOUNIAN_TURNTABLE_REWARD_TITLE, language);
            String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ZHOUNIAN_TURNTABLE_REWARD_CONTENT, language), num);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), content, title);
        }
    }

    @Override
    public boolean isActivityRunning() {
        ActivityInfo activityInfo = ZhouNianTurntableMgr.getInTimeActivityInfo();
        return ActivityMgr.activityInTime(activityInfo);
    }

}
